package com.ruoyi.web.controller.system;

import java.util.*;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Building;
import com.ruoyi.system.domain.Resident;
import com.ruoyi.system.domain.vo.RepairVo;
import com.ruoyi.system.service.IBuildingService;
import com.ruoyi.system.service.IResidentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Repair;
import com.ruoyi.system.service.IRepairService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 小区报修信息Controller
 * 
 * @author 祖新宇
 * @date 2024-12-18
 */
@Controller
@RequestMapping("/system/repair")
public class RepairController extends BaseController
{
    private String prefix = "system/repair";

    @Autowired
    private IRepairService repairService;

    @Resource
    private IResidentService residentService;
    @Resource
    private IBuildingService buildingService;

    @RequiresPermissions("system:repair:view")
    @GetMapping()
    public String repair()
    {
        return prefix + "/repair";
    }

    /**
     * 查询小区报修信息列表
     */
    @RequiresPermissions("system:repair:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Repair repair)
    {
        startPage();
        repair.setIsDelete(0);
        List<Repair> list = repairService.selectRepairList(repair);
        List<RepairVo> repairVos = new ArrayList<>();
        list.forEach(r -> {
            RepairVo repairVo = new RepairVo();
            BeanUtils.copyProperties(r, repairVo);
            Resident resident = residentService.selectResidentById(r.getResidentId());
            repairVo.setResidentName(Optional.of(resident).map(Resident::getName).orElse("--"));
            Building building = buildingService.selectBuildingById(r.getBuildingId());
            repairVo.setBuildingName(Objects.isNull(building) ? "--" : building.getBuildingName());
            repairVos.add(repairVo);
        });
        return getDataTable(repairVos);
    }

    /**
     * 导出小区报修信息列表
     */
    @RequiresPermissions("system:repair:export")
    @Log(title = "小区报修信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Repair repair)
    {
        List<Repair> list = repairService.selectRepairList(repair);
        ExcelUtil<Repair> util = new ExcelUtil<Repair>(Repair.class);
        return util.exportExcel(list, "小区报修信息数据");
    }

    /**
     * 新增小区报修信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<Resident> residents = residentService.selectResidentList(new Resident());
        mmap.put("residents", residents);
        List<Building> buildings = buildingService.selectBuildingList(new Building());
        mmap.put("buildings", buildings);
        return prefix + "/add";
    }

    /**
     * 新增保存小区报修信息
     */
    @RequiresPermissions("system:repair:add")
    @Log(title = "小区报修信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Repair repair)
    {
        repair.setCreateTime(new Date());
        repair.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        repair.setCreateBy(String.valueOf(userId));
        repair.setUpdateBy(String.valueOf(userId));
        return toAjax(repairService.insertRepair(repair));
    }

    /**
     * 修改小区报修信息
     */
    @RequiresPermissions("system:repair:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Repair repair = repairService.selectRepairById(id);
        mmap.put("repair", repair);
        List<Resident> residents = residentService.selectResidentList(new Resident());
        mmap.put("residents", residents);
        List<Building> buildings = buildingService.selectBuildingList(new Building());
        mmap.put("buildings", buildings);
        return prefix + "/edit";
    }

    /**
     * 修改保存小区报修信息
     */
    @RequiresPermissions("system:repair:edit")
    @Log(title = "小区报修信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Repair repair)
    {
        repair.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        repair.setUpdateBy(String.valueOf(userId));
        return toAjax(repairService.updateRepair(repair));
    }

    /**
     * 删除小区报修信息
     */
    @RequiresPermissions("system:repair:remove")
    @Log(title = "小区报修信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(repairService.softDeleteRepairByIds(ids));
    }
}
